<template>
	<view>
		<view class="nosd" v-if="PlysicalNo == 0 || sdStatus">
			<img src="https://statics.xmcsrv.net/weixin/warningsd.png" alt="">
			<view class="" style="margin: 10rpx;">
				{{sdStatus?'SD卡异常':'设备没有SD卡'}}
			</view>
		</view>
		<view class="" v-if="(PlysicalNo && PlysicalNo != 0) && !sdStatus">
			<j-cell-group>
				<j-cell title="存储容量" :more="false" :tips="totalCapacity+'G'"></j-cell>
				<j-cell title="录像分区" :more="false" :tips="videoTotalSpace+'G'"></j-cell>
				<j-cell title="图片分区" :more="false" :tips="imgTotalSpace+'G'"></j-cell>
				<j-cell title="剩余容量" :more="false" :tips="residueCapacity+'G'"></j-cell>
				<j-cell ref="jcell" title="录像满时" :more="false">
					<view slot="tips" class="laba">
						<uni-data-checkbox @change="OverWritechang" v-model="OverWrite" :localdata="OverWriteList">
						</uni-data-checkbox>
					</view>
				</j-cell>
			</j-cell-group>
			<view class="foot-button">
				<button style="z-index: 888;" class="block" @tap="nextstep()" type="operation">格式化</button>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				device: {},
				PlysicalNo: null,
				storageInfo: {},
				OverWriteList: [{
					text: '停止录像',
					value: 'StopRecord'
				}, {
					text: '循环录像',
					value: 'OverWrite'
				}],
				OverWrite: "StopRecord",
				imagePartition: {},
				videoPartition: {},
				imgTotalSpace: 0, //图片分区
				videoTotalSpace: 0, //录像分区
				totalCapacity: 0, //储存容量
				residueCapacity: 0,
				sdStatus: null, //存储卡状态
			}
		},
		onLoad(opt) {
			this.device = JSON.parse(opt.device)
			this.getinfo();
			this.getconfig()
		},
		methods: {
			nextstep() {
				uni.showModal({
					title: '提示',
					content: '该操作将删除存储卡上的所有文件，是否格式化',
					success: res => {
						if (res.confirm) {
							uni.showLoading();
							this.formatting('0')
							this.formatting('1')
						} else if (res.cancel) {}
					}
				});
			},
			// 格式化操作
			formatting(v) {
				let token = this.device.token;
				let data = {
					Name: 'OPStorageManager',
					OPStorageManager: {
						Action: 'Clear',
						SerialNo: 0,
						PartNo: v
					}
				};
				getApp().globalData.api.opdev(data, token, (result) => {
					// console.log(result, '系统时间');
					uni.hideLoading();
					this.getinfo()
				});
			},
			// 获取关于设备
			getinfo() {
				uni.showLoading({
					title: '获取设备存储信息...'
				});
				let token = this.device.token;
				const data = {
					Name: 'StorageInfo'
				};
				getApp().globalData.api.getinfo(data, token, (result) => {
					console.log('获取设备存储信息:', result.StorageInfo);
					uni.hideLoading();
					this.storageInfo = result.StorageInfo[0].Partition;
					this.PlysicalNo = result.StorageInfo[0].PlysicalNo;
					this.PartNumber = result.StorageInfo[0].PartNumber;
					this.sdStatus = this.storageInfo.find(el => el.Status != 0) ? this.storageInfo.find(el => el
						.Status != 0).Status : null
					console.log(this.sdStatus)
					if (this.sdStatus) return
					if (this.PartNumber == 2) {
						let videoPartition = this.storageInfo.find(el => el.DirverType == 0);
						let imagePartition = this.storageInfo.find(el => el.DirverType == 4);
						this.videoPartition = videoPartition;
						this.imagePartition = imagePartition;
						//录像分区容量
						let videoRemainSpace = this.numFilter(videoPartition.RemainSpace);
						this.videoTotalSpace = this.numFilter(videoPartition.TotalSpace);
						//图片分区容量
						let imgRemainSpace = this.numFilter(imagePartition.RemainSpace);
						this.imgTotalSpace = this.numFilter(imagePartition.TotalSpace);
						// 录像/图片已使用的容量
						let videoResidue = this.NumberAs(this.videoTotalSpace, videoRemainSpace, 'subtraction')
						let imgResidue = this.NumberAs(this.imgTotalSpace, imgRemainSpace, 'subtraction')
						// 存储总量
						this.totalCapacity = this.NumberAs(this.videoTotalSpace, this.imgTotalSpace, 'add')
						// 剩余容量
						this.residueCapacity = this.NumberAs(this.NumberAs(this.totalCapacity, videoResidue,
							'subtraction'), imgResidue, 'subtraction');
						console.log(this.residueCapacity)
					}
					// console.log(this.videoPartition, this.imagePartition, '分区数据')
				});
			},
			// 获取硬盘满时处理策略
			getconfig() {
				uni.showLoading();
				let token = this.device.token;
				let data = {
					Name: 'General.General',
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					uni.hideLoading()
					this.OverWrite = result['General.General'].OverWrite;
				});
			},
			// 截取当前数据到小数点后两位
			numFilter(value) {
				value = parseInt(value, 16) / 1024
				const realVal = parseFloat(value).toFixed(2);
				return realVal;
			},
			// 处理小数失精问题
			NumberAs(arg1, arg2, type) {
				var r1, r2, m, n
				try {
					r1 = arg1.toString().split('.')[1].length
				} catch (e) {
					r1 = 0
				}
				try {
					r2 = arg2.toString().split('.')[1].length
				} catch (e) {
					r2 = 0
				}
				m = Math.pow(10, Math.max(r1, r2))
				n = (r1 >= r2) ? r1 : r2
				if (type == 'add') {
					return ((arg1 * m + arg2 * m) / m).toFixed(n)
				} else if (type == 'subtraction') {
					return ((arg1 * m - arg2 * m) / m).toFixed(n)
				}
			},
			//硬盘满时处理策略
			OverWritechang(e) {
				uni.showLoading();
				let token = this.device.token;
				let data = {
					'Name': 'General.General',
					'General.General': {
						OverWrite: this.OverWrite
					}
				};
				getApp().globalData.api.setconfig(data, token, (result) => {
					console.log('设置参数：', result);
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style lang="scss">
	.nosd {
		text-align: center;
		margin-top: 200rpx;

		img {
			width: 200rpx;
			height: 200rpx;
			// border: 1px solid red;
		}
	}
</style>
